//
//  smcJaikuAPI.h
//  juhu
//
//  Created by Christian Martick on 26.07.07.
//  Copyright 2007 Christian Martick. All rights reserved.
//
//	jaiku api - recent posts of all jaiku users - public timeline sort of
//
//	this objects handle the users contacts posts using rss feed
//
//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <UIKit/UIKit.h>
#import "smcJaikuGlobals.h"
#import "smcAsyncConnection.h"
#import "smcJaikuRSSParser.h"
#import "NSDictionary+BSJSONAdditions.h"
#import "XMLRPCRequest.h"

@interface smcJaikuAPI : NSObject {
	id _delegate;
	NSMutableArray *_contacts;
	NSMutableDictionary *_streams;
	NSMutableIndexSet *_indexesNew;
	NSMutableIndexSet *_indexesOld;
	NSString * _personalKey;
	NSString * _username;
	BOOL _doingUpdate;
	BOOL _doingContactUpdate;
	BOOL _contactUpdateError;
	int _imagesStillToCatch;
	int _flickrStillToCatch;
}

// init
- (id) initWithUsername: (NSString *) username andPersonalKey: (NSString *) personalKey;

// general setters and getters
- (BOOL) setUsername: (NSString *) username;
- (NSString *) username;
- (BOOL) setPersonalKey: (NSString *) personalKey;
- (NSString *) personalKey;
- (BOOL) setDelegate: (id) delegate;
- (id) delegate;

// testing credentials
- (NSError *) testAccountCredentials;

// general help methods 
- (NSError *) missingUsernameOrPasswordErrorWithDomain: (NSString *) domain;
- (NSString *) urlEncodeValue: (NSString *) string;
- (NSMutableString *) makeCleanUTF8MutableStringFromData: (NSData *) theData;
@end

@interface smcJaikuAPI (messages)
	// posting a presence update/message
	- (NSError *) postMessage: (NSString *) message withLocation: (NSString *) location andIcon: (int) index generated: (BOOL) flag;
	// posting a comment	
	- (NSError *) postComment: (NSString *) comment to: (NSURL *) url;
	// delete a jaiku post or comment
	- (BOOL) canDelete: (NSURL *) url;
	- (NSError *) deletePostOrCommentWithURL: (NSURL *) url;
@end

@interface smcJaikuAPI (contacts)
	// contact managment
	- (NSArray *) friends;
	- (NSArray *) channels;
	- (BOOL) isFriend: (NSString *) nickname;
	- (NSError *) addAsFriend: (NSString *) nickname;
	- (NSError *) removeFriend: (NSString *) nickname;
	- (NSError *) updateFriendListSynchronously: (BOOL) flag;
@end

@interface smcJaikuAPI (streams)
	// the streams - public and private
	- (BOOL) beginUpdateForStream: (NSString *) stream;
	- (id) stream: (NSString *) streamSelector;
@end

@interface smcJaikuAPI (streamPostProcess)
	/* 
		handle this method as if they were private, 
		never invoke them directly!! 
	*/
	- (void) _makeIndexSetOfNewEntriesForStream: (NSString *) stream andOldArray: (NSArray*) old;
	- (void) _checkIfAllDoneForStream: (NSString *) stream;
	- (NSString *) _jsonURLStringExtend: (NSString *) stringURL;
	- (void) _flickrAddURLForStream: (NSString *) stream withData: (NSData *) data andNumber: (int) index;	
	- (void) _flickrImagesForStream: (NSString *) stream;
	- (void) _loadImagesForStream: (NSString *) stream andOldArray: (NSArray *) oldArray;
	- (NSString *) _addLinks: (NSString *) text;
	- (void) _processEntriesFor: (NSString *) stream;
	- (void) _streamProcessingForStream: (NSString *) stream withData: (NSData *) theData;
@end

@interface smcJaikuAPI (finishedDownloads)
	//all finished asynchronous downloads are delegate her
	- (void) downloadFromURL: (NSURL *) url didFinishWithData: (id) data andInfo: (id) info;
	- (void) downloadFromURL: (NSURL *) url didFailWithError: (NSError *) error andInfo: (id) info;
@end

@protocol smcJaikuAPIDelegate
	// delegate protocol: any delegate has to conform to this
	- (void) stream: (NSString*) stream updatedWithResult: (id) result andIndexSetForNewPosts: (id) indexSet;
	- (void) streamUpdateFailedWithError: (NSError *) error;
@end
